﻿using MaterialDesignThemes.Wpf;
using SqlBuilder;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Main.Domain
{
    internal class TableBoundUCViewModel: INotifyPropertyChanged
    {

        private readonly ISnackbarMessageQueue _snackbarMessageQueue;
        public TableBoundUCViewModel(ISnackbarMessageQueue snackbarMessageQueue)
        {
            _snackbarMessageQueue = snackbarMessageQueue ?? throw new ArgumentNullException(nameof(snackbarMessageQueue));

            LoadClipBoardCommand = new AnotherCommandImplementation(loadClipBoard);
            SelectTableCommand = new AnotherCommandImplementation(selectTable);
            SelectColumnCommand = new AnotherCommandImplementation(selectColumn);
            CreateSQLCommand = new AnotherCommandImplementation(createSQL);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private List<Column> selectedTableColumns;
        public List<Column> SelectedTableColumns
        {
            get { return selectedTableColumns; }
            set { selectedTableColumns = value;OnPropertyChanged(); }
        }
        private string selectedTableName;
        public string SelectedTableName
        {
            get { return selectedTableName; }
            set { selectedTableName = value; OnPropertyChanged(); }
        }

        private ColumnContrast selectedContrast;
        public ColumnContrast SelectedContrast
        {
            get { return selectedContrast; }
            set { selectedContrast = value; OnPropertyChanged(); }
        }
        private ObservableCollection<ColumnContrast> _columnContrasts =new ObservableCollection<ColumnContrast>();
        public ObservableCollection<ColumnContrast> ColumnContrasts
        {
            get { return _columnContrasts; }
            set { ColumnContrasts = value; OnPropertyChanged(); }
        }
        public ICommand LoadClipBoardCommand { get; }
        public ICommand SelectTableCommand { get; }
        public ICommand SelectColumnCommand { get; }
        public ICommand CreateSQLCommand { get; }
      
        private void selectColumn(object obj)
        {
            if (SelectedContrast == null)
                return;
            var column = obj as Column;
            SelectedContrast.SourceColumn=column;
            SelectedContrast.TableName = SelectedTableName;
        }



        private void createSQL(object obj)
        {
            Clipboard.SetText (Functions.CreateSql(this.ColumnContrasts));
            _snackbarMessageQueue.Enqueue("copied to clipboard");
        }

        private void loadClipBoard(object obj)
        {
            ColumnContrasts.Clear();
            var str = Clipboard.GetText();
            var rows = str.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < rows.Length; i++)
            {
                var cells = rows[i].Split('\t',StringSplitOptions.RemoveEmptyEntries);
                if (cells.Length < 2)
                    continue;
                ColumnContrast contrast = new ColumnContrast{
                    TableName = selectedTableName,
                    TargetColumn = cells[0],
                    TargetComment= cells[1]    
                };
                ColumnContrasts.Add(contrast);
            }
        }

        private void selectTable(object obj)
        {
            SelectTableWindow s =new SelectTableWindow();
            var res = s.ShowDialog()??false;
            if (res)
            {
                SelectedTableColumns = s.SelectTable.Columns;
                SelectedTableName = s.SelectTable.Name;
            }
        }


        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
